<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        // 所有的引用数据类型存的都是地址

        const obj1 = {
            a: 1,
            b: {
                b1: 2,
                b2: 3
            }
        }


        // const obj2 = { ...obj1 };


        // obj1.b.b1 = 22;

        // console.log(obj2);


        function deepCopy(obj) {
            const obj2 = {};

            for (let key in obj) {
                // 判断值是不是对象
                if (typeof obj[key] === 'object') {
                    obj2[key] = deepCopy(obj[key]);
                }
                else {
                    obj2[key] = obj[key];
                }

            }
            return obj2;
        }


        const obj2 = deepCopy(obj1);

        obj1.b.b1 = 22;

        console.log(obj2);


    </script>

</body>

</html>